def confirm_security(row,col,array):
    for i in range(row):
        if col == array[i] or abs(row-i) == abs(col-array[i]):
            return False
    return True

def create_chessboard(array):
    board=[]
    n=len(array)
    for i in range(n):
        board.append([' ']*n)
    for i in range(n):
        board[i][array[i]]='Q'
    return board

def display_chessboard(board):
    n=len(board)
    for i in range(n):
        print(board[i])
    print('-'*5*n)

def n_queens(n):
    count=0
    def queens(row,n,array):
        if row == n:
            nonlocal count
            count+=1
            print(count)
            display_chessboard(create_chessboard(array))
        else:
            for col in range(n):
                if confirm_security(row,col,array):
                    queens(row+1,n,array+[col])
    queens(0,n,[])

n_queens(8)
